package com.yk.spark.dao.file.impl;

import com.yk.spark.dao.common.api.ICommonDAO;
import com.yk.spark.dao.file.api.IFileDAO;
import com.yk.spark.model.FileModel;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by Limit on 2017/8/23.
 */
@Repository("fileDAO")
public class FileDAOImpl implements IFileDAO {

    @Resource(name = "commonDAO")
    private ICommonDAO commonDAO;

    @SuppressWarnings("unchecked")
    @Override
    public FileModel queryFileModel(FileModel model) {
        String hql= "from FileModel as model where model.fileId=:fileId";
        Query<FileModel> query=this.commonDAO.getSession().createQuery(hql);
        query.setParameter("fileId",model.getFileId());
        return query.getSingleResult();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<FileModel> queryFileModel() {
        String hql= "from FileModel as model order by model.updateTime desc ";
        Query<FileModel> query=this.commonDAO.getSession().createQuery(hql);
        return query.list();
    }
    @SuppressWarnings("unchecked")
    @Override
    public List<FileModel> queryFileModel(int start, int limit) {
        String hql= "from FileModel as model order by model.updateTime desc ";
        Query<FileModel> query=this.commonDAO.getSession().createQuery(hql);
        query.setFirstResult(start);
        query.setMaxResults(limit);
        return query.list();
    }

    @Override
    public long queryFileModelTotal() {

        String hql="select count(*) from FileModel";
        Query<Long> query=this.commonDAO.getSession().createQuery(hql,Long.class);
        return query.getSingleResult();
    }
}
